from django.db import models
from decimal import Decimal


class CurrencyField(models.DecimalField):
    __metaclass__ = models.SubfieldBase
    
    def __init__(self, *args, **kwargs):
        kwargs['max_digits'] =  8
        kwargs['decimal_places'] = 2
        super(CurrencyField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        try:
            return super(CurrencyField, self).to_python(value).quantize(Decimal('0.01'))
        except AttributeError:
            return None

from south.modelsinspector import add_introspection_rules
rules = [
  (
    (CurrencyField,),
    [],
    {
        "max_digits": ["max_digits", {"default": 8}],
        "decimal_places": ["decimal_places", {"default": 2}]
    },
  )
]
add_introspection_rules(rules, ["^festivalrock\.utils\.CurrencyField"])
